#include <stdio.h>
#include <stdlib.h>
#include <iconv.h>
#include <string.h>

#define OUTLEN 128

int display(char *incode, int len);
int display_better(char *incode, int len);

int main()
{
	char *string = "开源中国";//"青青子木";
	iconv_t cd;
	int inbuf_len = strlen(string);
	char outbuf[OUTLEN];
	char *pin = string;
	char *pout = &outbuf[0];
	int outbuf_len = OUTLEN;
	int i;

	memset(outbuf, 0, OUTLEN);
	printf("Originial Data:\n");
	printf("\tpin str: %s, outbuf str:%s\n", pin, outbuf);
	printf("\tinbuf_len=%d, outbuf_len=%d\n", inbuf_len, outbuf_len);
	printf("\tstrlen(outbuf)= %d\n", strlen(outbuf));

	cd = iconv_open("GB2312", "UTF8");
	if(cd == 0)
		return EXIT_FAILURE;
	int count = iconv(cd, &pin, &inbuf_len, &pout, &outbuf_len);
	printf("iconv count : %d\n", count);
	iconv_close(cd);

	printf("After Converted Data:\n");
	printf("\tpin str: %s, gb2312 str:%s\n", pin, outbuf );
	printf("\tinbuf_len=%d, outbuf_len=%d\n", inbuf_len, outbuf_len);
	printf("\tstrlen(outbuf)= %d\n", strlen(outbuf));
	
	for(i = 0; i < strlen(outbuf); i += 2)
	{
		//display(outbuf+i, 2);
		display_better (outbuf+i, 2);
	}

	return EXIT_SUCCESS;
}

int display(char *incode, int len)
{
	int    i,j;   
	FILE    *HZK=NULL; 
	char    mat[32]; 
	unsigned    char    qh,wh;   
	unsigned    long    offset;
	int arrar[16][16],m,n;
	unsigned char a=0;
	int ret = 0;

	memset(arrar, 0, sizeof(arrar));
	qh=incode[0]-0xa0;//获得区码            
	wh=incode[1]-0xa0;   //获得位码               
	offset=(94*(qh-1)+(wh-1))*32; /*得到偏移位置*/   
	printf("区码：%d,位码：%d\n",qh,wh);

	if((HZK=fopen("HZK16","rb")) ==NULL)  
	{   
	   perror("Can't Open hzk16");   
	   return (EXIT_FAILURE);   
	}   
	fseek(HZK,offset,SEEK_SET);   
	ret = fread(mat,32,1,HZK);//读取汉字的16*16点阵字模
	if(ret != 1)
	{
		perror("fread HZK");
		fclose(HZK);
		return -1;
	}

    fclose(HZK);

	//显示
	for(i=0;i<32;i++)
	{
		a=mat[i];
		m=i/2;
		n=i%2*8;
		for(j=7;j>=0;j--)
		{
			arrar[m][n+j]=a&0x01;
			a >>= 1;
	    }
	}
#if 0
	for(m=0;m<16;m++)
	{
		for(n=0;n<16;n++)
		   printf("%d",arrar[m][n]);
		printf("\n");
    }
#endif
	for(m=0;m<16;m++)
	{
		for(n=0;n<16;n++)
		{
			if(arrar[m][n]==0)
				printf("%c",32);
			else
				printf("*");
		}
        printf("\n");
    }
	return EXIT_SUCCESS;
}

int display_better(char *incode, int len)
{
	int i, j, k;
	char mat[16][2];
	FILE    *HZK=NULL; 
	unsigned    char    qh,wh;   
	unsigned    long    offset;
	int ret = 0;

	qh=incode[0]-0xa0;//获得区码            
	wh=incode[1]-0xa0;   //获得位码               
	offset=(94*(qh-1)+(wh-1))*32; /*得到偏移位置*/   
	printf("区码：%d,位码：%d\n",qh,wh);

	if((HZK=fopen("HZK16","rb")) ==NULL)  
	{   
	   perror("Can't Open hzk16");   
	   return (EXIT_FAILURE);   
	}   
	fseek(HZK,offset,SEEK_SET);   
	ret = fread(mat,32,1,HZK);//读取汉字的16*16点阵字模
	if(ret != 1)
	{
		perror("fread HZK");
		fclose(HZK);
		return -1;
	}
    fclose(HZK);

	for(i = 0; i < 16; i++)
	{
		for(j = 0; j < 2; j++)
			for(k = 0; k < 8; k++)
			{
				if(mat[i][j] & (0x80>>k))
					printf("*");
				else
					printf(" ");
			}
		printf("\n");
	}

	return EXIT_SUCCESS;
}

